﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using RenrenDotSDK;
using RenrenDotSDK.Model;
using Jayrock.Json.Conversion;

using System.Text.RegularExpressions;

namespace NJL.Web
{
    public partial class RenRenCallBack : BasePageVisitor
    {
        protected string AUTH_GUID;
        private string strFromWebSiteCode;
        private string strFromDomain;
        private string strFromUrl;
        private string strFromLoginType;

        RenrenApiClient rr = new RenrenApiClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["websitecode"] != null)
            {
                Session.Add("FromWebSiteCode", Request.QueryString["websitecode"]);
                Session.Add("FromDomain", Request.QueryString["domain"]);
                Session.Add("FromUrl", Request.QueryString["url"]);
                Session.Add("FromLoginType", Request.QueryString["logintype"]);
            }

            try
            {
                if (Request["code"] == null)
                {
                    rr.GetAuthorizationCode();
                }
                else
                {
                    var currentUser = rr.CallMethod("users.getInfo");

                    string strID = Regex.Match(currentUser, "\"uid\":([\\d]*),").Groups[1].Value;
                    string strName = Regex.Match(currentUser, "\"name\":\"(([\\u4e00-\\u9fa5]|[a-zA-Z0-9])*)\",").Groups[1].Value;

                    //var user = (RenrenDotSDK.Model.BasicProfile)JsonConvert.Import(typeof(RenrenDotSDK.Model.BasicProfile), currentUser);
                    strFromWebSiteCode = Session["FromWebSiteCode"].ToString();
                    strFromDomain = Session["FromDomain"].ToString();
                    strFromUrl = Session["FromUrl"].ToString();
                    strFromLoginType = Session["FromLoginType"].ToString();

                    if ("" != currentUser)
                    {
                        Model.NJL_USER model = new BLL.NJL_USER().GetModelByRenRenOpenID(strID);
                        if (null == model)
                        {
                            model = new Model.NJL_USER();
                            model.ID = System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                            model.NJL_SYS_BASE_ID = strFromWebSiteCode;
                            model.USER_NAME = strName;
                            model.RENREN_OPENID = strID;
                            model.TYPE_CODE = "2";//直接激活
                            model.INTEGRAL = 0;
                            model.CREATE_DATE_TIME = System.DateTime.Now;

                            new BLL.NJL_USER().Add(model);

                            //添加个人信息
                            Model.NJL_PERSON modelPerson = new Model.NJL_PERSON();
                            modelPerson.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                            modelPerson.NJL_SYS_BASE_ID = strFromWebSiteCode;
                            modelPerson.CREATE_DATE_TIME = DateTime.Now;
                            modelPerson.NJL_USER_ID = model.ID;
                            new BLL.NJL_PERSON().Add(modelPerson);

                            Session.Clear();
                            Session.Abandon();
                            Session.Add("userid", model.ID);
                            Session.Add("username", model.USER_NAME);
                            Session.Add("usertype", model.TYPE_CODE);

                            string userType = model.TYPE_CODE;
                            if (model.TYPE_CODE == "0")
                            {
                                model.TYPE_CODE = "2";
                                new BLL.NJL_USER().Update(model);
                            }
                            string strToURL = "";
                            //如果未激活先到激活页面
                            if (userType == "0")
                            {
                                strToURL = "/RegisterTow.aspx";
                            }
                            else if (strFromUrl == "")
                            {
                                strToURL = "/";
                            }
                            else if (strFromUrl != "")
                            {
                                strToURL = strFromUrl;
                            }
                            else
                            {
                                strToURL = "/";
                            }

                            ShareSession.ShareSessionFormatter.Serialize(ShareSession.ShareSessionFormatter.FormatterType.Soap);
                            AUTH_GUID = Session["AUTH_GUID"].ToString();

                            string strURL = "http://" + strFromDomain + "/GetUserSessions.aspx?AUTH_GUID=" + AUTH_GUID + "&logintype=" + strFromLoginType + "&tourl=" + strToURL;
                            Response.Redirect(strURL, false);
                        }
                        else
                        {
                            Session.Clear();
                            Session.Abandon();
                            Session.Add("userid", model.ID);
                            Session.Add("username", model.USER_NAME);
                            Session.Add("usertype", model.TYPE_CODE);

                            string userType = model.TYPE_CODE;
                            string strToURL = "";
                            //如果未激活先到激活页面
                            if (userType == "0")
                            {
                                strToURL = "/RegisterTow.aspx";
                            }
                            else if (strFromUrl == "")
                            {
                                strToURL = "/";
                            }
                            else if (strFromUrl != "")
                            {
                                strToURL = strFromUrl;
                            }
                            else
                            {
                                strToURL = "/";
                            }

                            ShareSession.ShareSessionFormatter.Serialize(ShareSession.ShareSessionFormatter.FormatterType.Soap);
                            AUTH_GUID = Session["AUTH_GUID"].ToString();

                            string strURL = "http://" + strFromDomain + "/GetUserSessions.aspx?AUTH_GUID=" + AUTH_GUID + "&logintype=" + strFromLoginType + "&tourl=" + strToURL;
                            Response.Redirect(strURL, false);
                        }
                    }
                    else
                    {
                        Response.Redirect("~/", true);
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }

    }
}
